//
//  TravelDetailTableViewController.m
//  游记详情
//
//  Created by lanou3g on 16/3/21.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TravelDetailTableViewController.h"
#import "OneTravelDetailsCell.h"
#import "TwoTravelDetailsCell.h"
#import "ThreeTravelDetailsCell.h"
#import "CDHttpHelper.h"
#import "SKModel.h"
#import "DetailsModel.h"
#import "UIImageView+WebCache.h"
#import "Singleton.h"

#define urlString @"https://chanyouji.com/api/trips/%ld.json"

@interface TravelDetailTableViewController ()

@end

@implementation TravelDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化数组
    self.allDataArray = [NSMutableArray array];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"OneTravelDetailsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"One"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TwoTravelDetailsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Two"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ThreeTravelDetailsCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"Three"];
    
    //解析数据
    [self handleData];
}

- (void)handleData{
    // 从前往后传id
    Singleton *single = [Singleton shareSingle];
    
    [[CDHttpHelper defaultCDHttpHelper] get:[NSString stringWithFormat:@"https://chanyouji.com/api/trips/%ld.json",single.ID] params:nil success:^(id responseObj) {
        NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObj options:NSJSONReadingMutableLeaves error:&error];
        
        // 直接赋值
        self.name = dictionary[@"name"];
        self.photos_count = [dictionary[@"photos_count"] intValue];
        self.start_date = dictionary[@"start_date"];
        self.end_date = dictionary[@"end_date"];
        
        // 获取大图
        self.front_cover_photo_url = dictionary[@"front_cover_photo_url"];
        
        // 获取个人头像
        NSDictionary *dict = dictionary[@"user"];
        self.image = dict[@"image"];
        
        // 获取详细信息
        for (NSDictionary *diction in dictionary[@"trip_days"]) {
            for (NSDictionary *dict in diction[@"nodes"]) {
                for (NSDictionary *dic in dict[@"notes"]) {
                    
                    // 获取文字
                    DetailsModel *model = [[DetailsModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    
                    // 获取图片
                    for (NSString *key in dic) {
                        if ([key isEqualToString:@"photo"]) {
                            NSDictionary *dic11 = dic[@"photo"];
                            [model setValuesForKeysWithDictionary:dic11];
                        }
                    }
                    [self.allDataArray addObject:model];
                }
            }
        }
        [self.tableView reloadData];

    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allDataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DetailsModel *model = [[DetailsModel alloc]init];
    model = self.allDataArray[indexPath.row];
    
    // 有图有文字
    if (model.Description != nil && model.url != nil) {
        OneTravelDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"One" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[OneTravelDetailsCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"One"];
        }
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.url]];
        cell.descriptionLabel.text = model.Description;
        
        cell.descriptionLabel.frame = CGRectMake(CGRectGetMinX(cell.descriptionLabel.frame), CGRectGetMinY(cell.descriptionLabel.frame), CGRectGetWidth(cell.descriptionLabel.frame), [self getTrueHeightWithDescription:model.Description]);
        
        return cell;
        
    // 没文字有图
    } else if(model.Description == nil && model.url != nil){
        TwoTravelDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Two" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[TwoTravelDetailsCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Two"];
        }
        [cell.picView sd_setImageWithURL:[NSURL URLWithString:model.url]];
        return cell;
        
    // 有文字没图
    }else{
        ThreeTravelDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Three" forIndexPath:indexPath];
        if (cell == nil) {
            cell = [[ThreeTravelDetailsCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Three"];
        }
        cell.descriptionLabel.text = model.Description;
        
        cell.descriptionLabel.frame = CGRectMake(CGRectGetMinX(cell.descriptionLabel.frame), CGRectGetMinY(cell.descriptionLabel.frame), CGRectGetWidth(cell.descriptionLabel.frame), [self getTrueHeightWithDescription:model.Description]);
        
        return cell;
    }
}


- (CGFloat)getTrueHeightWithDescription:(NSString *)discuss
{
    CGRect rect = [discuss boundingRectWithSize:(CGSizeMake([UIScreen mainScreen].bounds.size.width, 200000)) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailsModel *model = [[DetailsModel alloc]init];
    model = self.allDataArray[indexPath.row];
    
    if (model.Description != nil && model.url == nil) {
        return [self getTrueHeightWithDescription:model.Description] + 50;
    }else if(model.Description == nil && model.url != nil){
        return 350;
    }else{
        return [self getTrueHeightWithDescription:model.Description] + 50 + 350;
    }
}

@end
